package com.itheima01.day09;

import java.util.ArrayList;

/**
 * @author 刘全
 * @date 2021/7/10
 */
/*
需求：定义一个方法，方法接收一个集合对象（泛型为Student），方法内部将年龄低于18的学生对象找出
并存入新集合对象，方法返回新集合。
 */
public class ArrayList4 {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        Student stu1 = new Student("张三", 20);
        Student stu2 = new Student("李四", 18);
        Student stu3 = new Student("王五", 15);
        Student stu4 = new Student("刘六", 17);
        Student stu5 = new Student("孙七", 22);
        list.add(stu1);
        list.add(stu2);
        list.add(stu3);
        list.add(stu4);
        list.add(stu5);
        ArrayList<Student> list2 = get(list);
        for (int i = 0; i < list2.size(); i++) {
            System.out.println(list2.get(i).getName()+","+list2.get(i).getAge());
        }
    }

    public static ArrayList<Student> get(ArrayList<Student> list) {
        ArrayList<Student> list2 = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getAge() < 18) {
                list2.add(list.get(i));
            }
        }
        return list2;
    }
}
